import { Cart } from '../cart/cart-model.js';
import { Address } from '../../utils/address.js';
import {Order} from 'order-model.js';


var cart = new Cart();  //实例化，购物车
var order = new Order();
var address = new Address();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id : null
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var from = options.from;
    if (from=='cart'){
      this._fromCart(options.account);
    }
    else{
      var id = options.id;
      this._fromOrder(id);
    }
  },

  _fromCart: function (account){
    var productsArr;
    this.data.account = account;

    productsArr = cart.getCartDataFromLocal(true);

    this.setData({
      productsArr: productsArr,
      account: account,
      orderStatus: 0
    });

    /**显示收获地址 */
    address.getAddress((res) => {
      this._bindAddressInfo(res);
    });
  },

  /**使用id查找订单信息   onshow方法为使用订单编号查找订单信息 */
  _fromOrder:function(id){
    if (id) {
      var that = this;
      //下单后无论失败或者成功，点击左上角返回时能够更新订单状态所有放在onshow中
      order.getOrderInfoByOrderId(id, (data) => {
        that.setData({
          orderStatus: data.status,
          productsArr: data.products,
          account: data.totalPrice,
          basicInfo: {
            orderTime: data.createTime,
            orderNo: data.orderNo
          },
        });

        //快照地址
        var addressInfo = data.address;
        addressInfo.totalDetail = address.setAddressInfo(addressInfo);
        //绑定地址
        that._bindAddressInfo(addressInfo);
      });
    }
  },

  onShow:function(){
    if(this.data.id){
      var that = this;
      //下单后无论失败或者成功，点击左上角返回时能够更新订单状态所有放在onshow中

      var id = this.data.id;
      order.getOrderInfoById(id,(data)=>{
        that.setData({
          orderStatus : data.status,
          productsArr: data.products,
          account : data.totalPrice,
          basicInfo:{
            orderTime:data.createTime,
            orderNo:data.orderNo
          },
        });

        //快照地址
        var addressInfo =data.address;
        addressInfo.totalDetail = address.setAddressInfo(addressInfo);
        //绑定地址
        that._bindAddressInfo(addressInfo);
      });
    }
  },

  editAddress:function(event){
    var that = this;
    wx.chooseAddress({
      success : function(res){
        var addressInfo = {
          name : res.userName,
          mobile : res.telNumber,
          totalDetail: address.setAddressInfo(res)
        }
        that._bindAddressInfo(addressInfo);

        //保存地址
        address.submitAddress(res,(flag)=>{
          if(!flag){
            that.showTips('操作提示','地址信息更新失败!');
          }
        });
      }
    })
  },

  /**
   * 提示窗口
   * title ----标题
   * content ----正文
   * flag ----是否跳转到"我的"页面
   */
  showTips:function(title,content,flag){
    wx.showModal({
      title: title,
      content: content,
      showCancel : false,
      success:function(res){
        if(flag){
          wx.switchTab({
            url: '/pages/my/my',
          });
        }
      }
    });
  },

  /**绑定地址信息 */
    _bindAddressInfo:function(addressInfo){
      this.setData({
        addressInfo: addressInfo
      });
    },

    /**下单和付款 */
    pay:function(){
      if (!this.data.addressInfo){
        this.showTips('下单提示','请填写你的收获地址');
        return;
      }
      if(this.data.orderStatus == 0){
        this._firstTimePay();
      }else{
        this._oneMoresTimePay();
      }
    },

    /**第一次支付 */
    _firstTimePay:function(){
      var orderInfo = [],
          procuctInfo = this.data.productsArr,
          order = new Order();
      for (let i = 0; i < procuctInfo.length;i++){
            orderInfo.push({
              product_id: procuctInfo[i].id,
              counts: procuctInfo[i].counts
            });
          }

          var that = this;
          //支付分成两步，第一步生成订单号，然后根据订单号支付
          order.doOrder(orderInfo,(data)=>{
            //订单生成成功
            if(data.pass){
              //更新订单状态
              var id = data.order_id;
              that.data.id = id;
              that.data.fromCartFlag = false;
              //开始支付
              that._execpay(id);
            }else{
              that._orderFail(data);  //下单失败
            }
          });
    },

    /**开始支付   id为订单id */
    _execpay:function(id){
      var that = this;
      order.execPay(id,(statusCode)=>{
        if (statusCode !=0){
          that.deleteProducts();//将已经下单的商品从购物车删除
          var flag = statusCode == 2;
          wx.navigateTo({
            url: '../pay-result/pay-result?id='+id+'&flag='+flag+'&from=order',
          });
        }
      });
    },

    /*
      *下单失败
      * params:
      * data - {obj} 订单结果信息
      * */
    _orderFail: function (data) {
      var nameArr = [],
        name = '',
        str = '',
        pArr = data.pStatusArray;
      for (let i = 0; i < pArr.length; i++) {
        if (!pArr[i].haveStock) {
          name = pArr[i].name;
          if (name.length > 15) {
            name = name.substr(0, 12) + '...';
          }
          nameArr.push(name);
          if (nameArr.length >= 2) {
            break;
          }
        }
      }
      str += nameArr.join('、');
      if (nameArr.length > 2) {
        str += ' 等';
      }
      str += ' 缺货';
      wx.showModal({
        title: '下单失败',
        content: str,
        showCancel: false,
        success: function (res) {
        }
      });
    },

    /**将已经下单的商品从购物车中删除 */
  deleteProducts:function(){
    var ids = [], arr = this.data.productsArr;
    for(let i=0;i< arr.length;i++){
      ids.push(arr[i].id);
    }
    cart.delete(ids);
  },
 
})